package com.tb.stock

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.tb.common.base.BaseActivity
import com.tb.common.base.BaseFragment
import com.tb.common.widget.NoScrollViewPager
import com.tb.stock.databinding.ActivityMainBinding
import java.util.Arrays

/**
 * @ClassName: ActivityMain
 * @Description: 测试主页面
 * @Author: clark
 * @Date: 2022/2/28 5:27 下午
 */
class ActivityMain : BaseActivity() {
    var mainBinding: ActivityMainBinding? = null
    private var mPager: NoScrollViewPager? = null
    private val mFragments: MutableList<BaseFragment> = ArrayList()
    private var mAdapter: FragmentAdapter? = null
    private val positionList: MutableList<Int> = ArrayList()
    var onNavigationItemSelectedListener =
        BottomNavigationView.OnNavigationItemSelectedListener { item ->
            val i = item.itemId
            Config.itemKeyList.forEachIndexed { index, item ->
                if (i == item) {
                    mPager!!.currentItem = positionList[index]
                    return@OnNavigationItemSelectedListener true
                }
            }
            false
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainBinding = DataBindingUtil.setContentView(this@ActivityMain, R.layout.activity_main)
        mainBinding?.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        mPager = mainBinding?.containerPager
        mPager!!.offscreenPageLimit = 3
        for (i in Config.ListType[Config.type]) {
            val fragmentItem = ARouter.getInstance().build(i).navigation() as BaseFragment
            mFragments.add(fragmentItem)
        }
        mAdapter = FragmentAdapter(supportFragmentManager, mFragments)
        mainBinding?.setViewPaAdapter(mAdapter)
        mainBinding?.navigation?.inflateMenu(Config.menuList[Config.type])
        positionList.clear()
        positionList.addAll(Arrays.asList(*Config.positionList[Config.type]))
    }
}